package com.whj.servicemap.service.impl;

import com.whj.internalcommon.constant.AMapConfigConstant;
import com.whj.internalcommon.constant.CommonStatusEnum;
import com.whj.internalcommon.dto.DicDistrict;
import com.whj.internalcommon.dto.ResponseResult;
import com.whj.servicemap.mapper.DicDistrictMapper;
import com.whj.servicemap.remote.MapDicDistrictClient;
import com.whj.servicemap.service.DistrictService;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Auther: wanghaijun
 * @Date: 2023/1/10 - 01 - 10 - 13:28 
 * @Description: com.whj.servicemap.service.impl
 */

@Service
@Slf4j
public class DistrictServiceImpl implements DistrictService {

    @Autowired
    private MapDicDistrictClient mapDicDistrictClient;
    @Autowired
    private DicDistrictMapper dicDistrictMapper;

    /**
     * 根据地图服务 将地区字典存到数据库
     * @param keywords
     * @return
     */
    @Override
    public ResponseResult initDicDistrict(String keywords) {

        //调用地图服务
        String dicDistrictResult = mapDicDistrictClient.dicDistrict(keywords);

        //解析地图
        JSONObject districtsJsonObject = JSONObject.fromObject(dicDistrictResult);
        int status = districtsJsonObject.getInt(AMapConfigConstant.STATUS);
        if (status != 1) {
            return ResponseResult.fail(CommonStatusEnum.MAP_DISTRICT_ERROR.getCode(), CommonStatusEnum.MAP_DISTRICT_ERROR.getValue());
        }

        JSONArray countryJsonArray = districtsJsonObject.getJSONArray(AMapConfigConstant.DISTRICTS);
        for (int i = 0; i < countryJsonArray.size(); i++) {

            JSONObject countryJsonObject = countryJsonArray.getJSONObject(i);
            //获取json中的值
            String countryAddressCode = countryJsonObject.getString(AMapConfigConstant.ADCODE);
            String countryAddressName = countryJsonObject.getString(AMapConfigConstant.NAME);
            String countryParentAddressName = "0";
            String countryLevel = countryJsonObject.getString(AMapConfigConstant.LEVEL);
            //插入数据库
            insertDicDistrict(countryAddressCode, countryAddressName, countryParentAddressName, countryLevel);

            JSONArray provinceJsonArray = countryJsonObject.getJSONArray(AMapConfigConstant.DISTRICTS);
            for (int p = 0; p < provinceJsonArray.size(); p++) {

                JSONObject provinceJsonObject = provinceJsonArray.getJSONObject(p);
                //获取json中的值
                String provinceAddressCode = provinceJsonObject.getString(AMapConfigConstant.ADCODE);
                String provinceAddressName = provinceJsonObject.getString(AMapConfigConstant.NAME);
                String provinceParentAddressName = countryAddressCode;
                String provinceLevel = provinceJsonObject.getString(AMapConfigConstant.LEVEL);
                //插入数据库
                insertDicDistrict(provinceAddressCode, provinceAddressName, provinceParentAddressName, provinceLevel);


                JSONArray cityJsonArray = provinceJsonObject.getJSONArray(AMapConfigConstant.DISTRICTS);
                for (int c = 0; c < cityJsonArray.size(); c++) {

                    JSONObject cityJsonObject = cityJsonArray.getJSONObject(c);
                    //获取json中的值
                    String cityAddressCode = cityJsonObject.getString(AMapConfigConstant.ADCODE);
                    String cityAddressName = cityJsonObject.getString(AMapConfigConstant.NAME);
                    String cityParentAddressName = provinceAddressCode;
                    String cityLevel = cityJsonObject.getString(AMapConfigConstant.LEVEL);

                    //插入数据库
                    insertDicDistrict(cityAddressCode, cityAddressName, cityParentAddressName, cityLevel);

                    JSONArray districtJsonArray = cityJsonObject.getJSONArray(AMapConfigConstant.DISTRICTS);
                    for (int d = 0; d < districtJsonArray.size(); d++) {

                        JSONObject districtJsonObject = districtJsonArray.getJSONObject(d);
                        //获取json中的值
                        String districtAddressCode = districtJsonObject.getString(AMapConfigConstant.ADCODE);
                        String districtAddressName = districtJsonObject.getString(AMapConfigConstant.NAME);
                        String districtParentAddressName = cityAddressCode;
                        String districtLevel = districtJsonObject.getString(AMapConfigConstant.LEVEL);

                        //遇到街道就跳过
                        if(districtLevel.equals(AMapConfigConstant.STREET)){
                            continue;
                        }
                        //插入数据库
                        insertDicDistrict(districtAddressCode, districtAddressName, districtParentAddressName, districtLevel);

                    }
                }
            }
        }
        return ResponseResult.success("");
    }

    /**
     * 根据四个值来插入数据库
     *
     * @param addressCode
     * @param addressName
     * @param parentAddressName
     * @param level
     */
    public void insertDicDistrict(String addressCode, String addressName, String parentAddressName, String level) {
        DicDistrict dicDistrict = new DicDistrict();
        dicDistrict.setAddressCode(addressCode);
        dicDistrict.setAddressName(addressName);
        dicDistrict.setParentAddressCode(parentAddressName);
        dicDistrict.setLevel(generateInt(level));
        dicDistrictMapper.insert(dicDistrict);
    }

    /**
     * 比较字符串来确定返回的值
     *
     * @param level 区域名称
     * @return
     */
    public int generateInt(String level) {
        int levelInt = 0;
        if (level.trim().equals("country")) {
            levelInt = 0;
        } else if (level.trim().equals("province")) {
            levelInt = 1;
        } else if (level.trim().equals("city")) {
            levelInt = 2;
        } else if (level.trim().equals("district")) {
            levelInt = 3;
        }
        return levelInt;
    }

}
